import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2023/9/24
 */
public class Demo20 {
    public static void swap(char[] arr, int x, int y){
        char temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
    }
    public static int bfs(String start, String end){
        Map<String,Integer> map = new HashMap<>();//存储每种方式走过的距离

        Queue<String> q = new LinkedList<>();//队列, 用来存储内容

        q.offer(start);
        map.put(start,0);//表示初始状态的值对应map中value值对应0,表示还没有前进

        int[] dx = {-1,0,1,0};
        int[] dy = {0,1,0,-1};
        while(!q.isEmpty()){
            String t = q.poll();
            int k = t.indexOf('x');//找到x元素在String中的下标
            int x = k / 3; int y = k % 3;

            if(t.equals(end))return map.get(t);

            for(int i = 0; i < 4; i++){
                int a = x + dx[i],b = y + dy[i];
                if(a >= 0 && a < 3 && b >= 0 && b < 3){
                    char[] arr = t.toCharArray();
                    swap(arr,k,a*3+b);

                    String str = new String(arr);
                    if(map.get(str) == null){
                        map.put(str,map.get(t) + 1);
                        q.offer(str);
                    }
                }
            }
        }
        return -1;
    }

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String start = "";
        for(int i = 0; i < 9; i++){
            String s = scan.next();
            start += s;
        }
        String end = "12345678x";
        System.out.println(bfs(start,end));

    }
}
